import * as Common from './common.js'
import * as MsgMgr from './msgMgr.js'
import { DigTreasure } from './digTreasure.js';
import { NetApi } from './net/netApi.js';
var url = Common.UrlBase + Common.ApiEnums.UserMgr;
export class Util
{
    static getGoodsNameByGid(gid)
    {
        var gname = "";
        switch (parseInt(gid))
        {
            case 1:
                gname = "招兵令";
                break;
            case 2:
                gname = "招贤令";
                break;
            case 3:
                gname = "将军令";
                break;
            case 4:
                gname = "大将军令";
                break;
            case 5:
                gname = "体力丹";
                break;
            case 6:
                gname = "副将经验书";
                break;
            case 7:
                gname = "原矿";
                break;
            case 8:
                gname = "精矿";
                break;
            case 9:
                gname = "珍矿";
                break;
            case 10:
                gname = "1级技能书";
                break;
            case 11:
                gname = "2级技能书";
                break;
            case 12:
                gname = "3级技能书";
                break;
            case 13:
                gname = "4级技能书";
                break;
            case 14:
                gname = "5级技能书";
                break;
            case 15:
                gname = "小银袋";
                break;
            case 16:
                gname = "银两";

                break;
            case 17:
                gname = "金币";
                break;
        }
        return gname;

    }
    static getGoodsIdByGname(gname)
    {
        var gid = 0;
        switch (gname)
        {
            case "招兵令":
                gid = 1;
                break;
            case "招贤令":
                gid = 2;
                break;
            case "将军令":
                gid = 3;
                break;
            case "大将军令":
                gid = 4;
                break;
            case "体力丹":
                gid = 5;
                break;
            case "副将经验书":
                gid = 6;
                break;
            case "原矿":
                gid = 7;
                break;
            case "精矿":
                gid = 8;
                break;
            case "珍矿":
                gid = 9;
                break;
            case "1级技能书":
                gid = 10;
                break;
            case "2级技能书":
                gid = 11;
                break;
            case "3级技能书":
                gid = 12;
                break;
            case "4级技能书":
                gid = 13;
                break;
            case "5级技能书":
                gid = 14;
                break;
            case "小银袋":
                gid = 15;
                break;
            case "银两":
                gid = 16;
                break;
            case "金币":
                gid = 17;
                break;
        }
        return gid;

    }
    static getCurrentMsgType()
    {
        const triggerTabList = document.querySelectorAll('#myTab button[class="nav-link active"]')
        const ele = triggerTabList[0];
        if (ele.id == "home-tab")
            return 0;
        else if (ele.id == "profile-tab")
            return 1;
        else if (ele.id == "contact-tab")
            return 2;

        else if (ele.id == "disabled-tab")
            return 3;
    }
    static getMsgidByType(type)
    {
        if (type == 0)
            return "home-tab";
        else if (type == 1)
            return "profile-tab";
        else if (type == 2)
            return "contact-tab";
        else
            return "disabled-tab";
    }
    static getJobNameByJobId(jid)
    {
        var jname = "武士";
        var jnames = ["武士", "术士", "文人", "商人", "常人"];
        jname = jnames[parseInt(jid)];
        return jname;
    }


    static GetQueryString(name)
    {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg); //获取url中"?"符后的字符串并正则匹配
        var context = "";
        if (r != null)
            context = decodeURIComponent(r[2]);
        reg = null;
        r = null;
        return context == null || context == "" || context == "undefined" ? "" : context;
    }


    static getKvFromCookie(key)
    {
        var obj = JSON.parse($.cookie("user"));
        return obj[key];
    }
    static getKvFromCookieBag(key)
    {
        var ret = 0;
        if ($.cookie("bag") == undefined) return ret;
        var objlist = JSON.parse($.cookie("bag"));
        for (let index = 0; index < objlist.length; index++)
        {
            if (objlist[index].goods_id == key)
            {
                ret = objlist[index].counts;
                break;
            }
        }
        return ret;;
    }
    static setKvFromCookieUser(key, val)
    {
        var userobj = JSON.parse($.cookie("user"));
        userobj[key] = val;
        $.cookie("user", JSON.stringify(userobj));
    }

    //update userinfo
    static setKvFromCookieBag(key, val)
    {
        var baglist = JSON.parse($.cookie("bag"));
        for (let index = 0; index < baglist.length; index++)
        {
            if (baglist[index]["goods_id"] == key)
            {
                baglist[index]["counts"] = parseInt(val);
                break;
            }
        }
        $.cookie("bag", JSON.stringify(baglist));
    }

    static generateRandomInteger(min, max)
    {
        return Math.floor(Math.random() * (max - min + 1)) + min;
    }

    static mytime()
    {
        let time = new Date();
        let timeInfo =
            (time.getHours() + ':' + time.getMinutes() + ':' + time.getSeconds())
        return timeInfo;
    }
    static getCountsAndGidObj(index)
    {

        var retjson = "";
        var baoshiTypes = ["钻石", "白玉", "翡翠", "珍珠", "琥珀", "水晶"];
        var yuanshiTypes = ["原矿", "精矿", "珍矿"];
        var kuangshiTypes = ["金矿", "银矿", "铁矿"];
        var baowuTypes = ["招兵令", "招贤令", "大将军令", "将军令", "小银袋", "体力丹", "副将经验书"];
        if (index == 0)
        {
            var counts = Util.generateRandomInteger(100, 5000);
            var gid = Util.getGoodsIdByGname("银两");
            retjson = "{\"counts\":" + counts + ",\"gid\":" + gid + "}";

        }

        else if (index == 1)
        {
            var counts = Util.generateRandomInteger(5, 40);
            var gid = Util.getGoodsIdByGname("金币");
            retjson = "{\"counts\":" + counts + ",\"gid\":" + gid + "}";
        }

        else if (index == 2)
        {
            var counts = 1;
            var gid = Util.getGoodsIdByGname(Util.generateRandomInteger(1, 5) + "级技能书");
            retjson = "{\"counts\":" + counts + ",\"gid\":" + gid + "}";

        }

        else if (index == 3 || index == 4)
        {
            var counts = Util.generateRandomInteger(100, 5000);
            var gid = Util.getGoodsIdByGname("银两");
            retjson = "{\"counts\":" + counts + ",\"gid\":" + gid + "}";

        }

        //return "一个"+generateRandomInteger(1,6)+"级"+baoshiTypes[generateRandomInteger(0,baoshiTypes.length-1)];
        else if (index == 5)
        {
            var counts = 1;
            var gid = Util.getGoodsIdByGname(yuanshiTypes[Util.generateRandomInteger(0, yuanshiTypes.length - 1)]);
            if (gid == 9)
            {
                MsgMgr.sendMsg("1000", "系统", Util.getKvFromCookie("name") + "运气太好了,挖到一个珍矿", 3, () =>
                {
                    MsgMgr.getMsgByType(3);
                });
            }
            retjson = "{\"counts\":" + counts + ",\"gid\":" + gid + "}";

        }

        else if (index == 6)
        {
            var counts = 1;
            var gid = Util.getGoodsIdByGname(baowuTypes[Util.generateRandomInteger(0, baowuTypes.length - 1)]);
            retjson = "{\"counts\":" + counts + ",\"gid\":" + gid + "}";

        }

        return retjson;
    }
    //发给服务器增加物品
    static netSendAddGoods(gid, counts)
    {
        var curcounts = Util.getKvFromCookieBag(gid);
        NetApi.NetHttpAddUserGoods(gid, counts, function (res)
        {
            console.log("NetHttpAddUserGoods res:" + res);
            if (parseInt(gid) == 16 || parseInt(gid) == 17)
            {
                var gidi = parseInt(gid);
                var countsbase = Util.getKvFromCookie(gidi == 16 ? "tael" : "coin");
                console.log(countsbase);
                console.log(counts);
                var newcounts = parseInt(counts) + parseInt(countsbase);
                console.log("newcounts:" + newcounts);
                NetApi.NetHttpUpdateUserKvByid(gidi, newcounts, function (res)
                {
                    Util.updateCookieFromNet();
                })
            }
        })
    }
    static updateCookieFromNet()
    {
        var user_id = $.cookie('user_id');

        $.post(
            url,
            {
                "action": "getUserInfoAll",
                "user_id": user_id
            },
            function (res)
            {
                console.log(res);
                const resobj = JSON.parse(res);
                console.log(resobj.data[0])
                var ele = (resobj.data)[0];
                DigTreasure.counts = parseInt(ele.phystr);
                document.getElementById("tips").innerText = "体力：" + DigTreasure.counts;
                $.cookie("user", JSON.stringify(ele), { expires: 7 });
            }
        );
    }
    static getOutStr(index)
    {
        var obj = JSON.parse(Util.getCountsAndGidObj(index));

        Util.netSendAddGoods(obj.gid, obj.counts)
        if (index == 0 || index == 1 || index == 3 || index == 4)
        {

            return obj.counts + Util.getGoodsNameByGid(obj.gid);
        } else
        {
            return "一个" + Util.getGoodsNameByGid(obj.gid);
        }
    }
}